<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="display" uri="http://displaytag.sf.net/el" %>

<portlet:defineObjects/>

<link href="/Todo-portlet/css/todo.css" rel="stylesheet" type="text/css" />

<table width="100%">
    <tr><td align="right" valign="middle" height="35px" bgcolor="#6d5d54">
        <portlet:actionURL var="searchUrl">
                <portlet:param name="action" value="searchTodo"/>
        </portlet:actionURL>
        <form action="${searchUrl}" method="POST">
                <input type="text" name="searchText"/>&nbsp;
                <input type="submit" name="searchButton" value="Search"/>&nbsp;
        </form>
    </td></tr>
    <tr><td align="right" valign="middle">
        <div id="menu">&#091;
        <a href="<portlet:actionURL>
                    <portlet:param name="action" value="todos"/>
                 </portlet:actionURL>">Home
        </a>&#093;&nbsp;&#091;
        <a href="<portlet:renderURL>
                    <portlet:param name="action" value="editCategory"/>
                 </portlet:renderURL>">Add category
        </a>&#093;&nbsp;&#091;
        <a href="<portlet:renderURL>
                    <portlet:param name="action" value="editTodo"/>
                    <portlet:param name="categoryId" value="${selectedCategory}"/>
                 </portlet:renderURL>">Add todo
        </a>&#093;</div>
    </td></tr>
    <c:choose>
        <c:when test="${mode == 'todos'}">
            <!-- Todos mode: show todos -->
            <tr style="height: 30px; vertical-align: middle;">
                <td>
                    <c:choose>
                        <c:when test="${selectedCategory != -1}">
                            Current category&nbsp;&gt;&nbsp;<u>${selectedCategoryName}</u>
                        </c:when>
                        <c:otherwise>
                            Search term&nbsp;&gt;&nbsp;<u>${searchText}</u>
                        </c:otherwise>
                    </c:choose>
                </td>
            </tr>
            <tr>
                <td>
                    <c:choose>
                         <c:when test="${not empty todos}">
                             <div class="scrolling">
                                 <display:table name="todos" id="todo" style="width: 100%; margin-top: 3px;">
                                    <display:column title="" sortable="false">
                                        <portlet:renderURL var="detailUrl">
                                        <portlet:param name="action"
                                              value="todoDetail"/>
                                        <portlet:param name="todoId"
                                              value="${todo.id}"/>
                                        </portlet:renderURL>
                                        <portlet:renderURL var="editUrl">
                                          <portlet:param name="action"
                                              value="editTodo"/>
                                          <portlet:param name="todoId"
                                              value="${todo.id}"/>
                                        </portlet:renderURL>
                                        <portlet:actionURL var="deleteUrl">
                                          <portlet:param name="action"
                                              value="deleteTodo"/>
                                          <portlet:param name="todoId"
                                              value="${todo.id}"/>
                                        </portlet:actionURL>
                                        <a href="${detailUrl}"><img src=
                                            "/Todo-portlet/images/view.gif" border="0"
                                            title="View todo details"/></a>
                                        <a href="${editUrl}"><img src=
                                            "/Todo-portlet/images/edit.gif" border="0"
                                            title="Edit todo"/></a>
                                        <a href="${deleteUrl}"><img src=
                                            "/Todo-portlet/images/trash.gif" border="0"
                                            title="Delete todo"/></a>
                                    </display:column>
                                    <display:column title="Status" sortable="true">
                                        <input type="checkbox" disabled="disabled" ${todo.done ? 'checked' : ''}/>
                                    </display:column>
                                    <display:column title="Title" property="title" sortable="true"/>
                                    <display:column title="Priority" sortable="true">
                                          <c:choose>
                                            <c:when test="${todo.priority == 1}">High</c:when>
                                            <c:when test="${todo.priority == 2}">Medium</c:when>
                                            <c:otherwise>Low</c:otherwise>
                                            </c:choose>
                                    </display:column>
                                    <display:column title="Creation Date" property="creationDate" format="{0,date,yyyy-MM-dd}" sortable="true"/>
                                </display:table>
                             </div>
                        </c:when>
                    <c:otherwise>No todo found!</c:otherwise>
                </c:choose>
            </td>
          </tr>
        </c:when>
        <c:otherwise>
            <!-- Categories mode: Show all categories -->
            <tr style="height: 30px; vertical-align: middle;">
                <td>Categories</td>
            </tr>
            <tr>
                <td>
                    <div class="scrolling">
                        <table style="margin-top: 3px; width: 100%">
                            <c:forEach var="category" items="${categories}">
                                <tr>
                                    <c:choose>
                                        <c:when test="${category.owner != 'Public'}">
                                             <td width="60px" align="center">
                                               <portlet:renderURL var="editUrl">
                                                        <portlet:param name="action" value="editCategory"/>
                                                        <portlet:param name="categoryId" value="${category.id}"/>
                                                    </portlet:renderURL>
                                                    <portlet:actionURL var="deleteUrl">
                                                        <portlet:param name="action" value="deleteCategory"/>
                                                        <portlet:param name="categoryId" value="${category.id}"/>
                                                    </portlet:actionURL>
                                                    <a href="${editUrl}"><img src="/Todo-portlet/images/edit.gif" border="0" title="Edit category"/></a>
                                                    <a href="${deleteUrl}"><img src="/Todo-portlet/images/trash.gif" border="0" title="Delete category"/></a>
                                            </td>
                                        </c:when>
                                        <c:otherwise><td width="40px" align="center"><img src="/Todo-portlet/images/public.gif" border="0" title="Public category"/></c:otherwise>
                                    </c:choose>
                                    <td>
                                         <div id="link">
                                            <portlet:actionURL var="todosUrl">
                                                <portlet:param name="action" value="todos"/>
                                                <portlet:param name="categoryId" value="${category.id}"/>
                                            </portlet:actionURL>
                                            <a href="${todosUrl}">${category.name}</a>
                                         </div>
                                    </td>
                               </tr>
                            </c:forEach>
                        </table>
                    </div>
                </td>
            </tr>
        </c:otherwise>
    </c:choose>
</table>